// @include "../../../scripts/spket.js"

(function() {
	var timeLog = panda.log("proxy.log.time");
	var aroundLog = panda.log("proxy.log.around");

	var timeString = "{}.{}() completed in {} seconds.";
	var inputString = "Excuting {}.{}(), with param: {}.";
	var outputString = "{}.{}() completed, with result: {}.";

	proxy.log = { priority: 100 };

	proxy.log.time = {
		priority: 80,
		expr: /^(api|page|dbo)\./,
		func: function(name, method, args) {
			var start = new Date().getTime();
			var ret = this[method].apply(this, args);
			var seconds = (new Date().getTime() - start) / 1000;

			timeLog.info(timeString, [ name, method, seconds ]);
			return ret;
		}
	}

	proxy.log.around = {
		priority: 100,
		expr: /^(api|page|dbo)\./,
		func: function(name, method, args) {
			var toLog = JSON.stringify(args[0]);
			aroundLog.info(inputString, [name, method, toLog]);

			var ret = this[method].apply(this, args);

			toLog = JSON.stringify(shorten());
			aroundLog.info(outputString, [name, method, toLog]);

			return ret;

			function shorten() {
				if (ret instanceof Array && ret.length > 3) {
					return ret.slice(0, 3).concat("...");
				} else if (typeof ret === "string" && ret.length > 100) {
					return ret.slice(0, 100).concat("...");
				} else {
					return ret;
				}
			}
		}
	}
}());
